<template>
  <el-container>
    <div class="abs">
      <TextTree :tree="data" @leaf-click="leafClick"></TextTree>
    </div>
  </el-container>
</template>

<script>
import TextTree from "../../components/TextTree";
import BeiDou from "./data-statistics/beiDou";
import QiXiang from "./data-statistics/qiXiang";
import YanTu from "./data-statistics/yanTu";

export default {
  name: "DataStatistics",
  components: {
    TextTree,
  },
  props: {},
  data() {
    return {
      data: null,
    };
  },
  created() {
    let data = window.localStorage.getItem("treeData");
    data = JSON.parse(data);
    this.fun(data.treeData);
  },
  methods: {
    fun(arr) {
      let kkRes = arr;
      let siderList = [];
      for (let i = 0; i < kkRes.length; i++) {
        siderList.push({
          label: kkRes[i].monitorName,
          children: [
            { label: "北斗监测", children: [] },
            { label: "岩土监测", children: [] },
            { label: "气象监测", children: [] },
          ],
        });
        let siderListChildren = kkRes[i].sensorList;
        for (let k = 0; k < siderListChildren.length; k++) {
          siderListChildren[k].moduleEnum === "00" &&
          siderListChildren[k].sensorStatus === "10"
            ? siderList[i].children[0].children.push({
                id: siderListChildren[k].id,
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-weixing-copy",
                color: "green",
                sensorNumber: siderListChildren[k].sensorNumber,
                component: BeiDou,
              })
            : siderListChildren[k].moduleEnum === "00" &&
              siderListChildren[k].sensorStatus === "20"
            ? siderList[i].children[0].children.push({
                id: siderListChildren[k].id,
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-weixing-copy",
                color: "yellow",
                sensorNumber: siderListChildren[k].sensorNumber,
                component: BeiDou,
              })
            : siderListChildren[k].moduleEnum === "00" &&
              siderListChildren[k].sensorStatus === "30"
            ? siderList[i].children[0].children.push({
                id: siderListChildren[k].id,
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-weixing-copy",
                color: "grey",
                sensorNumber: siderListChildren[k].sensorNumber,
                component: BeiDou,
              })
            : siderListChildren[k].moduleEnum === "01" &&
              siderListChildren[k].sensorStatus === "10" &&
              siderListChildren[k].dataType === "01"
            ? siderList[i].children[1].children.push({
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-shan",
                color: "green",
                sensorNumber: siderListChildren[k].sensorNumber,
                id: siderListChildren[k].id,
                component: YanTu,
              })
            : siderListChildren[k].moduleEnum === "01" &&
              siderListChildren[k].sensorStatus === "10" &&
              siderListChildren[k].dataType === "02"
            ? siderList[i].children[1].children.push({
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-shan",
                color: "green",
                sensorNumber: siderListChildren[k].sensorNumber,
                id: siderListChildren[k].id,
                component: YanTu,
              })
            : siderListChildren[k].moduleEnum === "01" &&
              siderListChildren[k].sensorStatus === "10" &&
              siderListChildren[k].dataType === "04"
            ? siderList[i].children[1].children.push({
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-shan",
                color: "green",
                sensorNumber: siderListChildren[k].sensorNumber,
                id: siderListChildren[k].id,
                component: YanTu,
              })
            : siderListChildren[k].moduleEnum === "01" &&
              siderListChildren[k].sensorStatus === "10" &&
              siderListChildren[k].dataType === "05"
            ? siderList[i].children[1].children.push({
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-shan",
                color: "green",
                sensorNumber: siderListChildren[k].sensorNumber,
                id: siderListChildren[k].id,
                component: YanTu,
              })
            : siderListChildren[k].moduleEnum === "01" &&
              siderListChildren[k].sensorStatus === "20" &&
              siderListChildren[k].dataType === "01"
            ? siderList[i].children[1].children.push({
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-shan",
                color: "yellow",
                sensorNumber: siderListChildren[k].sensorNumber,
                id: siderListChildren[k].id,
                component: YanTu,
              })
            : siderListChildren[k].moduleEnum === "01" &&
              siderListChildren[k].sensorStatus === "20" &&
              siderListChildren[k].dataType === "02"
            ? siderList[i].children[1].children.push({
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-shan",
                color: "yellow",
                sensorNumber: siderListChildren[k].sensorNumber,
                id: siderListChildren[k].id,
                component: YanTu,
              })
            : siderListChildren[k].moduleEnum === "01" &&
              siderListChildren[k].sensorStatus === "20" &&
              siderListChildren[k].dataType === "04"
            ? siderList[i].children[1].children.push({
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-shan",
                color: "yellow",
                sensorNumber: siderListChildren[k].sensorNumber,
                id: siderListChildren[k].id,
                component: YanTu,
              })
            : siderListChildren[k].moduleEnum === "01" &&
              siderListChildren[k].sensorStatus === "20" &&
              siderListChildren[k].dataType === "05"
            ? siderList[i].children[1].children.push({
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-shan",
                color: "yellow",
                sensorNumber: siderListChildren[k].sensorNumber,
                id: siderListChildren[k].id,
                component: YanTu,
              })
            : siderListChildren[k].moduleEnum === "01" &&
              siderListChildren[k].sensorStatus === "30" &&
              siderListChildren[k].dataType === "01"
            ? siderList[i].children[1].children.push({
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-shan",
                color: "grey",
                sensorNumber: siderListChildren[k].sensorNumber,
                id: siderListChildren[k].id,
                component: YanTu,
              })
            : siderListChildren[k].moduleEnum === "01" &&
              siderListChildren[k].sensorStatus === "30" &&
              siderListChildren[k].dataType === "02"
            ? siderList[i].children[1].children.push({
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-shan",
                color: "grey",
                sensorNumber: siderListChildren[k].sensorNumber,
                id: siderListChildren[k].id,
                component: YanTu,
              })
            : siderListChildren[k].moduleEnum === "01" &&
              siderListChildren[k].sensorStatus === "30" &&
              siderListChildren[k].dataType === "04"
            ? siderList[i].children[1].children.push({
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-shan",
                color: "grey",
                sensorNumber: siderListChildren[k].sensorNumber,
                id: siderListChildren[k].id,
                component: YanTu,
              })
            : siderListChildren[k].moduleEnum === "01" &&
              siderListChildren[k].sensorStatus === "30" &&
              siderListChildren[k].dataType === "05"
            ? siderList[i].children[1].children.push({
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-shan",
                color: "grey",
                sensorNumber: siderListChildren[k].sensorNumber,
                id: siderListChildren[k].id,
                component: YanTu,
              })
            : siderListChildren[k].moduleEnum === "02" &&
              siderListChildren[k].sensorStatus === "10"
            ? siderList[i].children[2].children.push({
                id: siderListChildren[k].id,
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-qixiang",
                color: "green",
                sensorNumber: siderListChildren[k].sensorNumber,
                component: QiXiang,
              })
            : siderListChildren[k].moduleEnum === "02" &&
              siderListChildren[k].sensorStatus === "20"
            ? siderList[i].children[2].children.push({
                id: siderListChildren[k].id,
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-qixiang",
                color: "yellow",
                sensorNumber: siderListChildren[k].sensorNumber,
                component: QiXiang,
              })
            : siderList[i].children[2].children.push({
                id: siderListChildren[k].id,
                label: siderListChildren[k].sensorName,
                icon: "iconfont icon-qixiang",
                color: "grey",
                sensorNumber: siderListChildren[k].sensorNumber,
                component: QiXiang,
              });
        }
      }
      function isClick(obj) {
        if (obj.id == sessionStorage.getItem("sensorId")) {
          obj.default = true;
          return obj;
        }
        for (let i in obj) {
          if (obj.children) {
            isClick(obj.children);
          } else if (Array.isArray(obj)) {
            obj.forEach((item) => {
              isClick(item);
            });
          }
        }
      }
      isClick(siderList);
      this.data = siderList;
    },
    leafClick(d, node, tree) {
      sessionStorage.setItem("sensorId", d.id);
    },
  },
};
</script>

<style lang='less' scoped>
.el-container {
  width: 100%;
  height: 100%;
  .abs {
    position: absolute;
    left: 0;
    top: 6.4%;
    width: 100%;
    height: 93.6%;
  }
}
</style>
